//
//  BKGDBecomeVIPViewController.m
//  bestkeep
//
//  Created by 武探 on 2016/12/9.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKGDBecomeVIPViewController.h"
#import "Mediator+BKMy.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"

@interface BKGDBecomeVIPViewController ()<UIWebViewDelegate, NJKWebViewProgressDelegate,UIGestureRecognizerDelegate>

@property(nonatomic,strong) UIWebView *webView;
@property(nonatomic,strong) NSString *URLString;
@property(nonatomic,strong) NJKWebViewProgressView *webViewProgressView;
@property(nonatomic,strong) NJKWebViewProgress *webViewProgress;

@property(nonatomic,weak) id<UIGestureRecognizerDelegate> popDelegate;

@end

@implementation BKGDBecomeVIPViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    _popDelegate = self.navigationController.interactivePopGestureRecognizer.delegate;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = _popDelegate;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeComponent];
}

-(void)initializeComponent {
    
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width, 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
    
    _webView = [[UIWebView alloc] init];
    _webView.delegate = _webViewProgress;
    _webView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:_webView];
    
    [self makeConstraints];
    
    if(self.URLString) {
        [self loadWebpage];
    } else {
        [self requestWebData];
    }
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"RootNavigationBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTouch:)];
    [leftBarButton setImageInsets:UIEdgeInsetsMake(5, -1, 0, 1)];
    
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

-(void)backButtonTouch:(UIBarButtonItem*)sender {
    if(_webView.canGoBack) {
        [_webView goBack];
    } else {
        _webViewProgressView.hidden = YES;
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)makeConstraints {
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
}

-(void)requestWebData {
    
    NSDictionary *userInfo = [[Mediator sharedInstance] BKMyUserInfo];
    NSString *tgt = userInfo[BKMyUserGetTGTKey];
    if(!tgt || [tgt isEqualToString:@""]) {
        return;
    }
    
    NSString *stURL = [NSString stringWithFormat:@"%@m1/tickets/%@", BKGDUTOUUURL, tgt];
    NSString *validateURL = [NSString stringWithFormat:@"%@profile/open-vip", BKGDWebURL];
    NSDictionary *parameters = @{@"service": validateURL};
    
    __weak typeof(self) weakSelf = self;
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:stURL parameters:parameters success:^(id  _Nullable responseObject) {
       
        NSString *st = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *webpageURL = [NSString stringWithFormat:@"%@profile/open-vip?ticket=%@", BKGDWebURL, st];
        
        if(!weakSelf) {
            return;
        }
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        strongSelf.URLString = webpageURL;
        [strongSelf loadWebpage];
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        NSLog(@"%@",error);
    }];
}

-(void)loadWebpage {
    if(!self.URLString) {
        return;
    }
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.URLString]];
    [_webView loadRequest:request];
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
}


#pragma -mark webView delegate

-(void)webViewDidStartLoad:(UIWebView *)webView {
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    
    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
    [_webView stringByEvaluatingJavaScriptFromString:[self javaScript]];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    
    return YES;
}

/**
 *  JavaScript
 */
-(NSString*)javaScript {
    NSString *javaScript = @"document.documentElement.style.webkitUserSelect='none';\
    document.documentElement.style.webkitTouchCallout='none';\
    ";
    return javaScript;
}

-(void)dealloc {
    NSLog(@"VIPWebViewController dealloc");
}

@end
